package com.hoshiicloud.user.service.impl.organization;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.organization.PlatOrganizationDepartmentes;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.organization.PlatOrganizationDepartmentesMapper;
import com.hoshiicloud.user.service.organization.PlatOrganizationDepartmentesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
/**
 * <p>
 * 租户机构部门 服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-05-27
 */
@Slf4j
@Service
public class PlatOrganizationDepartmentesServiceImpl extends ServiceImpl<PlatOrganizationDepartmentesMapper, PlatOrganizationDepartmentes> implements PlatOrganizationDepartmentesService {
    @Autowired
    private IdGeneratorService idGeneratorService;
    /**
     * @return IPage
     * @description: 分页查询租户机构部门
     */
    @Override
    @Logable(authName = "机构（供应商）管理-租户机构部门管理-分页查询租户机构部门")
    @Cacheable(cacheNames = "PlatOrganizationDepartmentes",key ="#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public IPage<PlatOrganizationDepartmentes> getPage(IPage<PlatOrganizationDepartmentes> page, QueryWrapper<PlatOrganizationDepartmentes> queryWrapper,String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }


    /**
    * @param id
    * @return com.hoshiicloud.organization.entity.PlatOrganizationDepartmentes
    * @description: 根据id查询租户机构部门
    */
    @Override
    @Cacheable(cacheNames = "PlatOrganizationDepartmentes", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "机构（供应商）管理-租户机构部门 -查询租户机构部门 ")
    public PlatOrganizationDepartmentes getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增租户机构部门
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PlatOrganizationDepartmentes", allEntries = true)
    @Logable(authName = "机构（供应商）管理-租户机构部门-新增租户机构部门")
    public String insert(PlatOrganizationDepartmentes entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return id;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改租户机构部门
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PlatOrganizationDepartmentes", allEntries = true)
    @Logable(authName = "机构（供应商）管理-租户机构部门管理-修改租户机构部门")
    public boolean updateById(PlatOrganizationDepartmentes entity) {
        entity.setModifiedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PlatOrganizationDepartmentes", allEntries = true)
    @Logable(authName = "机构（供应商）管理-租户机构部门管理-删除租户机构部门")
    public boolean deleteById(Long id) {
        PlatOrganizationDepartmentes entity = this.getById(id);
        if(null == entity){
            log.error("删除平台机构部门异常，无法找到平台机构部门记录");
            throw new ServiceException("删除平台机构部门异常，无法找到平台机构部门记录");
        }
        entity.setModifiedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Override
    @Cacheable(cacheNames = "PlatOrganizationDepartmentes", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "机构（供应商）管理-租户机构部门管理-根据id集合查询租户机构部门")
    public List<PlatOrganizationDepartmentes> getByIds(List<Long> ids,String key) {
        return baseMapper.selectBatchIds(ids);
    }
}
